package linkedlist;

public class Josepfu {

    public static void main(String[] args) {

    }
}

//创建环形单向链表

class CircleSingleLinkedList{
    //创建一个first节点
    private Boy first = new Boy(-1);

    //添加，构建环形链表
    public void addBoy(int nums){
        //nums做数据校验
        if (nums < 2){
            System.out.println("nums的值不正确");
            return;
        }
        Boy curBoy = null;//辅助指针，帮助创建环形链表
        //使用for创建环形
        for (int i = 1; i < nums; i++){
            //根据编号创造节点
            Boy boy = new Boy(i);
            //如果是第一个
            if (i == 1){
                first = boy;
                first.setNext(first);//构成环状
                curBoy = first;
            }else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }
    //遍历
    public void list(){
        //判断链表是否为空
        if (first == null){
            System.out.println("没有任何元素");
            return;
        }
        Boy curBoy = first;
        while (true){
            System.out.println(curBoy.getNo());
            if (curBoy.getNext() == first){//最后一个节点
                break;
            }
            curBoy = curBoy.getNext();
        }
    }

    //根据用户输入，计算出圈顺序

    /**
     *
     * @param start：从第几开始读数
     * @param countNum：一次读多少个
     * @param nums：初始有多少个
     */
    public void countBoy(int start, int countNum, int nums){
        //先对数据校验
        if (first == null || start < 1 || start > nums){
            System.out.println("重新输入");
            return;
        }
        //创建辅助指针
        Boy helper = first;
        while (true){
            if (helper.getNext() == first){//说明helper指向最后一个节点
                break;
            }
            helper = helper.getNext();
        }
        for (int j = 0; j < start - 1; j++) {
            first = first.getNext();
            helper = helper.getNext();
        }

        while (true){
            if (helper == first){
                break;
            }
            for (int j = 0; j < countNum - 1; j++){
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.println(first.getNo());
            helper.setNext(first.getNext());
            first = first.getNext();
        }
    }
}

class Boy{
    private int no;
    private Boy next;

    public Boy(int no){
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}
